/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package util.components;

import java.awt.*;
import java.awt.image.BufferedImage;
import javax.swing.BorderFactory;
import javax.swing.GroupLayout;
import javax.swing.ImageIcon;
import javax.swing.JComponent;
import javax.swing.UIManager;

/**Para que funcione se debe setear en el componente padre que no sea resizable 
 * verticalmente
 *
 * @author raul
 */
public class CollapsiblePanel extends javax.swing.JPanel {

    private JComponent content;
    private GroupLayout layout;
    private boolean expanded = true;
    
    private ImageIcon iconExpanded = 
            new javax.swing.ImageIcon(CollapsiblePanel.class.getResource("/util/components/section_expanded.png"));
    private ImageIcon iconCollapsed = 
            new javax.swing.ImageIcon(CollapsiblePanel.class.getResource("/util/components/section_collapsed.png"));
    /**
     * Creates new form CollapsiblePanel
     */
    public CollapsiblePanel() {
        initComponents();
        content = contentPnl;
        setearFuente();
    }
    
    public void setExpanded(boolean expanded) {
        if (this.expanded != expanded) {
            this.expanded = expanded;
            content.setVisible(expanded);
            expansorBtn.setSelected(expanded);
            if (expanded) {
                expansorBtn.setIcon(iconExpanded);
            } else {
                expansorBtn.setIcon(iconCollapsed);
            }
        }
    }
    
    public boolean isExpanded() {
        return expanded;
    }
    
    private void setearFuente(){
        Font labelFont = UIManager.getFont("Label.font");
        tituloLbl.setFont(labelFont.deriveFont(Font.BOLD, labelFont.getSize()+2f));
        tituloLbl.setForeground(new Color(47, 75, 102));
    }
    
    private float[] coloresHsb(){
        Color titleColor = new Color(127, 157, 185, 1);
        return Color.RGBtoHSB(
                titleColor.getRed(), titleColor.getGreen(), titleColor.getBlue(), null);
    }
    
    public JComponent getContent() {
        return content;
    }

    public void setContent(JComponent content) {
        content.setBorder(BorderFactory.createLineBorder(Utilities.deriveColorHSB(getBackground(), 0, 0, -.3f)));
        layout.replace(this.content, content);
        this.content = content;
    }

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        
        Dimension size = getSize();
        Graphics2D g2 = (Graphics2D)g.create();
        g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, 
                RenderingHints.VALUE_ANTIALIAS_ON);
        
        Color color = Utilities.deriveColorHSB(getBackground(), 0, 0, -.3f);
        
        g2.setColor(Utilities.deriveColorAlpha(color, 40));        
        g2.drawRoundRect(0, 0 + 2, size.width - 1, size.height - 3, 10, 10);
        g2.setColor(Utilities.deriveColorAlpha(color, 90));        
        g2.drawRoundRect(0, 0 + 1, size.width - 1, size.height - 2, 10, 10); 
        g2.setColor(Utilities.deriveColorAlpha(color, 255));        
        g2.drawRoundRect(0, 0, size.width - 1, size.height - 1, 10, 10);

        g2.dispose();
    }
    
    public void setTitulo(String titulo) {
        tituloLbl.setText(titulo);
    }
    
    public String getTitulo() {
        return tituloLbl.getText();
    }
    
    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    private void initComponents() {

        tituloPnl = new javax.swing.JPanel(){

            @Override
            protected void paintComponent(Graphics g) {
                Dimension size = getSize();
                float hsb[] = coloresHsb();
                BufferedImage titleImage = Utilities.createTranslucentImage(size.width, size.height);
                GradientPaint gradient = new GradientPaint(0, 0,
                    Color.getHSBColor(hsb[0]-.013f, .15f, .85f), 0, 1,
                    Color.getHSBColor(hsb[0]-.005f, .24f, .80f), false);

                Graphics2D g2 = (Graphics2D)titleImage.getGraphics();
                g2.setPaint(gradient);
                g2.fillRoundRect(0, 0, this.getWidth(), this.getHeight()+5, 10, 10);
                g2.setColor(Utilities.deriveColorHSB(
                    Color.getHSBColor(hsb[0]-.005f, .24f, .80f), 0, 0, -.2f));
                g2.drawLine(1, this.getHeight() - 1, getWidth() - 2, this.getHeight() - 1);
                g2.setColor(Utilities.deriveColorHSB(
                        Color.getHSBColor(hsb[0]-.005f, .24f, .80f), 0, -.5f, .5f));
                g2.drawLine( 1, this.getHeight(), this.getWidth() - 2, this.getHeight());
                g2.setPaint(new GradientPaint(0, 0, new Color(0.0f, 0.0f, 0.0f, 1.0f),
                this.getWidth(), 0, new Color(0.0f, 0.0f, 0.0f, 0.0f)));
                g2.setComposite(AlphaComposite.DstIn);
                g2.fillRect(0, 0, this.getWidth(), this.getHeight());
                g2.dispose();
                g.drawImage(titleImage, 0, 0, null);
                super.paintComponent(g);
            }

            @Override
            public boolean isOpaque() {
                return false;
            }
        };
    expansorBtn = new javax.swing.JToggleButton();
    tituloLbl = new javax.swing.JLabel();
    contentPnl = new javax.swing.JPanel();

    setBorder(null);

    tituloPnl.setOpaque(false);

    expansorBtn.setIcon(iconExpanded); // NOI18N
    expansorBtn.setBorderPainted(false);
    expansorBtn.setContentAreaFilled(false);
    expansorBtn.addActionListener(new java.awt.event.ActionListener() {
        @Override
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            expansorBtnActionPerformed(evt);
        }
    });

    tituloLbl.setText("Titulo");

    javax.swing.GroupLayout tituloPnlLayout = new javax.swing.GroupLayout(tituloPnl);
    tituloPnl.setLayout(tituloPnlLayout);
    tituloPnlLayout.setHorizontalGroup(
        tituloPnlLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(tituloPnlLayout.createSequentialGroup()
            .addContainerGap()
            .addComponent(tituloLbl)
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 234, Short.MAX_VALUE)
            .addComponent(expansorBtn, javax.swing.GroupLayout.PREFERRED_SIZE, 24, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addContainerGap())
    );
    tituloPnlLayout.setVerticalGroup(
        tituloPnlLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
        .addGroup(tituloPnlLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.CENTER)
            .addComponent(tituloLbl)
            .addComponent(expansorBtn, javax.swing.GroupLayout.PREFERRED_SIZE, 28, javax.swing.GroupLayout.PREFERRED_SIZE))
    );
    
    
    contentPnl.setBorder(BorderFactory.createLineBorder(Utilities.deriveColorHSB(getBackground(), 0, 0, -.3f)));

    javax.swing.GroupLayout contentPnlLayout = new javax.swing.GroupLayout(contentPnl);
    contentPnl.setLayout(contentPnlLayout);
    contentPnlLayout.setHorizontalGroup(
        contentPnlLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGap(0, 0, Short.MAX_VALUE)
    );
    contentPnlLayout.setVerticalGroup(
        contentPnlLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGap(0, 2, Short.MAX_VALUE)
    );

    layout = new javax.swing.GroupLayout(this);
    this.setLayout(layout);
    layout.setHorizontalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addComponent(tituloPnl, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        .addComponent(contentPnl, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
    );
    layout.setVerticalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(layout.createSequentialGroup()
            .addComponent(tituloPnl, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
//            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
            .addComponent(contentPnl, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
    );
    }

    private void expansorBtnActionPerformed(java.awt.event.ActionEvent evt) {
        setExpanded(!expanded);
    }

    
    private javax.swing.JPanel contentPnl;
    private javax.swing.JToggleButton expansorBtn;
    private javax.swing.JLabel tituloLbl;
    private javax.swing.JPanel tituloPnl;
}
